package com.ipzoe.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.ipzoe.app.dto.HotCityResponseDTO;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.core.page.TableDataInfo;
import com.ipzoe.system.domain.CountryCode;
import com.ipzoe.system.domain.HotCity;
import com.ipzoe.system.domain.Region;
import com.ipzoe.system.service.ICountryCodeService;
import com.ipzoe.system.service.IHotCityService;
import com.ipzoe.system.service.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/region")
@Validated
@Slf4j
@Api(tags = "城市相关")
public class RegionController extends BaseController {

    @Autowired
    private IRegionService iRegionService;
    @Autowired
    private IHotCityService iHotCityService;
    @Autowired
    private ICountryCodeService iCountryCodeService;

    @PostMapping("list")
    @ApiOperation("查询城市列表")
    public AjaxResult list(@RequestParam(required = false, defaultValue = "false") boolean includeHotCity)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        if(includeHotCity){
            LambdaQueryWrapper<HotCity> hotCityLqw = new LambdaQueryWrapper<>();
            hotCityLqw.orderByAsc(HotCity::getOrderNum);
            List<HotCity> hotCityList = iHotCityService.list(hotCityLqw);
            ajaxResult.put("hotCityList", hotCityList);
        }

        LambdaQueryWrapper<Region> regionLqw = new LambdaQueryWrapper<>();
        regionLqw.eq(Region::getLevel, 3).orderByAsc(Region::getLetter);
        List<Region> regionList = iRegionService.list(regionLqw);
        ajaxResult.put("cityList", regionList);

        return ajaxResult;
    }

    @GetMapping("detail/{cityCode}")
    @ApiOperation("查询城市")
    public AjaxResult detail(@PathVariable Integer cityCode){
        Region region = iRegionService.getById(cityCode);
        return AjaxResult.success(region);
    }

    @GetMapping("countries")
    @ApiOperation("所有国家列表")
    public AjaxResult listCountry(){
        LambdaQueryWrapper<Region> regionLqw = new LambdaQueryWrapper<>();
        regionLqw.eq(Region::getLevel, 1).orderByAsc(Region::getId);
        List<Region> countryList = iRegionService.list(regionLqw);
        return AjaxResult.success(countryList);
    }

    @GetMapping("countryCodes")
    @ApiOperation("所有国家区号列表")
    public AjaxResult listCountryCode(){
        LambdaQueryWrapper<CountryCode> ccLqw = new LambdaQueryWrapper<>();
        ccLqw.orderByAsc(CountryCode::getSort);
        List<CountryCode> countryCodeList = iCountryCodeService.list(ccLqw);
        return AjaxResult.success(countryCodeList);
    }
}
